package com.coding.controller;

import com.coding.base.pojo.BaseModel;
import com.coding.pojo.Video;
import com.coding.service.OssImagesService;
import com.coding.service.VideoService;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @auther wangchenghao
 * @date 2020/7/26
 **/

@Controller
@ResponseBody
@RequestMapping("video")
public class VideoController {
    @Autowired
    VideoService videoService;
    @Autowired
    OssImagesService ossImagesService;

    @ApiOperation(value = "上传视频至oss")
    @RequestMapping(value = "/upVideo", method = RequestMethod.POST, consumes = "multipart/*", headers = "content-type=multipart/form-data")
    @ResponseBody
    public BaseModel upvideo(@ApiParam(value = "上传的文件", required = true) MultipartFile file, HttpServletRequest request) {
        BaseModel model = new BaseModel();
        try {
            Video video = videoService.setVideo(file);
            videoService.addVideo(video, model);
        } catch (Exception e) {
            e.printStackTrace();
            model.setFailure("上传视频失败");
        }

        return model;
    }

    @ApiOperation(value = "根据video_id查询视频")
    @RequestMapping(value = "/getVideoById", method = {RequestMethod.GET})
    public BaseModel getvideobyid(@ApiParam(value = "视频id", required = true) @RequestParam("video_id") int video_id) {
        BaseModel model = new BaseModel();
        try {
            videoService.getVideobyid(video_id, model);
        } catch (Exception e) {
            e.printStackTrace();
            model.setFailure("获取视频失败");
        }
        return model;
    }

    @ApiOperation(value = "根据name查询视频")
    @RequestMapping(value = "/getVideoByName", method = {RequestMethod.GET})
    public BaseModel getvideobyname(@ApiParam(value = "视频id", required = true) @RequestParam("name") String name) {
        BaseModel model = new BaseModel();
        try {
            videoService.getVideobyname(name, model);
        } catch (Exception e) {
            e.printStackTrace();
            model.setFailure("获取视频列表失败");
        }

        return model;
    }

    @ApiOperation(value = "根据tag查询视频")
    @RequestMapping(value = "/getVideoByTag", method = {RequestMethod.GET})
    public BaseModel getvideobytag(@ApiParam(value = "视频id", required = true) @RequestParam("name") String tag) {
        BaseModel model = new BaseModel();
        try {
            videoService.getVideobytag(tag, model);
        } catch (Exception e) {
            e.printStackTrace();
            model.setFailure("获取视频列表失败");
        }

        return model;
    }
}
